package com.hsnn.medstgmini.pro.project.controller;

import java.lang.reflect.Type;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hsnn.medstgmini.Constants;
import com.hsnn.medstgmini.base.std.model.HospInfo;
import com.hsnn.medstgmini.pro.project.service.BidProjhospitalManager;
import com.hsnn.medstgmini.sys.model.SysUser;
import com.hsnn.medstgmini.util.Pagination;
import com.hsnn.medstgmini.util.PinYin;

/**
 * @category 医疗机构库
 * @author 
 */
@Controller
@RequestMapping("/ProjhospEdit")
public class BidProjhospitalController {
	
	private static final Logger log = Logger.getLogger(BidProjhospitalController.class);
	
	@Autowired
	private BidProjhospitalManager bidProjhospitalManager;
	
	@Autowired
	private HttpServletRequest request;
	
	/**
	 * @category 医疗机构列表页面
	 * @param model
	 * @return
	 */
	@RequestMapping("/toList")
	public String hospitalList(Model model) {
		return "/pro/project/bidProjhospEdit";
	}
	
	
//	/**
//	 * 
//	 *@category 查询采购项目列表
//	 *@author 
//	 *@date 2015年6月11日 上午11:48:38
//	 *@param projCode
//	 *@param name
//	 *@param status
//	 *@return
//	 */
//	@RequestMapping("/getDataList")
//	@ResponseBody
//	public Pagination getDataList(String projCode,String name,String status){
//		Pagination page = new Pagination(request);
//		if (null != projCode && !"".equals(projCode.trim())){
//			page.getConditions().put("projCode", projCode.trim());
//		}
//		
//		if (null != name && !"".equals(name.trim())){
//			
//			page.getConditions().put("name", name.trim());
//		}
//		if (null != status && !"".equals(status.trim())){
//			page.getConditions().put("status", status.trim());
//		}
//		return bidProjhospitalManager.getDataList(page);
//	}
	
	
	@RequestMapping("/addHost")
	@ResponseBody
	public String addHost(String porjId,String hospId){
		return bidProjhospitalManager.addHost(porjId, hospId);
	}
	
	@RequestMapping("/batchAddHosp")
	@ResponseBody
	public Pagination batchAddHosp(String array){
		Pagination page = new Pagination(request);
		try {
			if (array != null && !"".equals(array)) {
				Gson gson = new Gson();
				Type type = new TypeToken<List<HospInfo>>() {
				}.getType();
				List<HospInfo> list = gson.fromJson(array, type);
				if (list != null && !list.isEmpty()) {
					for (HospInfo dp : list) {
						dp.setUpdUser("ccl");
					}
					bidProjhospitalManager.batchAddHosp(list);
					page.setSuccess(true);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			page.setSuccess(false);
		}
		return page;
	
	}
	
/**
 * 
 * @category 获取医疗机构列表
 * @Auther xugp
 * @date 2015年6月13日 下午9:20:32
 * @return
 * @return Pagination
 */
	@RequestMapping("/getHospDataList")
	@ResponseBody
	public Pagination getHospDataList(){
		Pagination page = new Pagination(request);
		String projId = request.getParameter("projId");
		if (null != projId && !"".equals(projId.trim())){
			page.getConditions().put("projId", projId.trim());
		}
		if (null != request.getParameter("areaId") && !"".equals(request.getParameter("areaId").trim())){
			page.getConditions().put("areaId", request.getParameter("areaId").trim());
		}
		page.getConditions().put("fahterId", request.getParameter("fahterId"));
		page.getConditions().put("status", request.getParameter("status"));
		if (null != request.getParameter("hospName") && !"".equals(request.getParameter("hospName").trim())){
			if (PinYin.isPinYin(request.getParameter("hospName").trim())){
				page.getConditions().put("hospSpelCode", request.getParameter("hospName").trim());
			} else {
				page.getConditions().put("hospName", request.getParameter("hospName").trim());
			}
		}
		if (null != request.getParameter("hospClassify") && !"".equals(request.getParameter("hospClassify").trim())){
			page.getConditions().put("hospClassify", request.getParameter("hospClassify").trim());
		}
		if (null != request.getParameter("hospCode") && !"".equals(request.getParameter("hospCode").trim())){
			page.getConditions().put("hospCode", request.getParameter("hospCode").trim());
		}
		return bidProjhospitalManager.getHospList(page);
	}
	

	
	
	/**
	 * @category 启用|停用
	 * @param hospital
	 * @return
	 */
	
	@RequestMapping("/changeStatus")
	@ResponseBody
	public Pagination changeStatus(HospInfo hospInfo) {
		Pagination page = new Pagination();
		try {
			if (hospInfo != null && hospInfo.getHospId() != null) {
				hospInfo.setUpdUser("ccc");
				bidProjhospitalManager.changeStatus(hospInfo);
				page.setSuccess(true);
			}
		} catch (Exception e) {
			log.error("",e);
			page.setSuccess(false);
		}
		return page;
	}

	/**
	 * @category 批量(启用|停用)
	 * @param array
	 * @return
	 */
	@RequestMapping("/batchChangeStatus")
	@ResponseBody
	public Pagination batchChangeStatus(String array) {
		Pagination page = new Pagination();
		try {
			if (array != null && !"".equals(array)) {
				Gson gson = new Gson();
				Type type = new TypeToken<List<HospInfo>>() {
				}.getType();
				List<HospInfo> list = gson.fromJson(array, type);
				SysUser user=(SysUser) request.getSession().getAttribute(Constants.USERINFO);
				if (list != null && !list.isEmpty()) {
					for (HospInfo hi : list) {
						hi.setUpdUser(user.getId());
					}
					bidProjhospitalManager.batchChangeStatus(list);
					page.setSuccess(true);
				}
			}
		} catch (Exception e) {
			log.error("",e);
			page.setSuccess(false);
		}
		return page;
	}

	

	

}
